package cn.cyl.org.service.impl;

import cn.cyl.basic.service.impl.BaseServiceImpl;
import cn.cyl.basic.util.Md5Utils;
import cn.cyl.basic.util.StrUtils;
import cn.cyl.org.domain.Department;
import cn.cyl.org.domain.Employee;
import cn.cyl.org.mapper.DepartmentMapper;
import cn.cyl.org.mapper.EmployeeMapper;
import cn.cyl.org.service.EmployeeService;
import cn.cyl.user.domain.Logininfo;
import cn.cyl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：部门
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee > implements EmployeeService {


    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 员工添加同步到logininfo表
     * @param employee
     */
    @Override
    @Transactional
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);

        employeeMapper.addRole(employee);


    }

    /**
     * 员工删除同步删除logininfo
     * @param id
     */
    @Override
    public void del(Long id) {
        //先查询登陆信息id
        Employee employee = employeeMapper.loadOne(id);
        //删除logininfo
        logininfoMapper.del(employee.getLogininfo_id());
        employeeMapper.delRoleByEmployeeId(id);
        super.del(id);

    }


    /**
     *修改员工同步修改logininfo表
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //注意:修改的时候不应该修改密码 - 强制修改密码

        //创建Logininfo对象:拷贝了6个数据包括id[employee的id]
        Logininfo logininfo = employee2Logininfo(employee);
        Employee dbEmployee = employeeMapper.loadOne(employee.getId());
        //重新设置Logininfoid
        logininfo.setId(dbEmployee.getLogininfo_id());
        Logininfo dbLogininfo = logininfoMapper.loadOne(dbEmployee.getLogininfo_id());
        logininfo.setDisable(dbLogininfo.getDisable());

        //操作数据库
        logininfoMapper.update(logininfo);
        //employee - 前端传递的
        employee.setShop_id(dbEmployee.getShop_id());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        super.update(employee);

        //修改 = 先删再添加
        employeeMapper.delRoleByEmployeeId(employee.getId());
        employeeMapper.addRole(employee);
    }
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //通过属性同名原则进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;
    }

    /**
     * 批量删除同步logininfo
     */
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    /**
     * 无限级数
     * @return
     */
    @Override
    public List<Department> deptTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        List<Department> departments = departmentMapper.loadAll();
        //2.根据查出来的id和对象，保存到map中
        HashMap<Long, Department> map = new HashMap<>();
        //循环添加进map
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        //前端需要得到的集合数据 - 部门树
        List<Department> deptTree=new ArrayList<>();
        for (Department department : departments) {
            if(department.getParent_id()==null){//顶级部门
                deptTree.add(department);
            }else {//子部门
                //1.想办法找到当前部门的 父部门
                Long parent_id = department.getParent_id();
                //2.根据map用key查出对应的value
                Department department1 = map.get(parent_id);
                //3.再将自己添加进父部门的Children里
                if(department1!=null){
                    department1.getChildren().add(department);
                }
            }
        }
        System.out.println(deptTree);
        return deptTree;
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {

        return employeeMapper.queryByShopId(shopId);
    }

}
